package com.ruoyi.web.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.mapper.VideoDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/resultTemplate")
public class ResultTemplateController extends BaseController {


    @Autowired
    private VideoDataMapper videoDataMapper;


    @GetMapping("getResultTemplateList")
    @ResponseBody
    public AjaxResult getResultTemplateList(Integer page,Integer limit,String keyword){
        // 处理默认值
        if (page == null || page <= 0) {
            page = 1;
        }
        if (limit == null || limit <= 0) {
            limit = 10;
        }

        // 计算起始位置
        Integer offset = (page - 1) * limit;

        // 查询数据
        List<Map<String, Object>> list = videoDataMapper.selectResultTemplateList(offset, limit, keyword);
        Integer total = videoDataMapper.selectResultTemplateCount(keyword);

        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("total",total);
        return AjaxResult.success(map);
    }


    @PostMapping("saveResultTemplate")
    @ResponseBody
    public AjaxResult saveResultTemplate(@RequestBody JSONObject jsonObject){

        try {
            // 转换为Map
            Map<String, Object> template = new HashMap<>();
            boolean isUpdate = false;

            if (jsonObject.containsKey("id") && jsonObject.get("id") != null) {
                Integer id = jsonObject.getInteger("id");
                template.put("id", id);
                isUpdate = true;

                // 验证模板是否存在
                Map<String, Object> existingTemplate = videoDataMapper.selectResultTemplateById(id);
                if (existingTemplate == null) {
                    return AjaxResult.error("模板不存在");
                }
            }

            template.put("templateName", jsonObject.getString("templateName"));
            template.put("templateContent", jsonObject.getString("templateContent"));

            if (isUpdate) {
                 videoDataMapper.updateResultTemplate(template);
            } else {
                 videoDataMapper.insertResultTemplate(template);
            }

            return AjaxResult.success("操作成功！");
        } catch (Exception e) {
            return AjaxResult.error("保存模板失败：" + e.getMessage());
        }
    }


    @GetMapping("/deleteResultTemplate")
    @ResponseBody
    public AjaxResult deleteResultTemplate(Integer id) {
        try {
            // 验证模板是否存在
            Map<String, Object> existingTemplate = videoDataMapper.selectResultTemplateById(id);
            if (existingTemplate == null) {
                return AjaxResult.error("模板不存在");
            }

            videoDataMapper.deleteResultTemplateById(id);

            return AjaxResult.success("操作成功！");
        } catch (Exception e) {
            return AjaxResult.error("删除模板失败：" + e.getMessage());
        }
    }

}
